<template>
  <el-dialog class="assign-role" title="分配角色" :visible="showRoleDialog" @close="closeShow" @open="openDialog">
    <!-- 这里准备复选框 -->
    <el-checkbox-group v-model="checkList" v-loading="loading">
      <el-checkbox v-for="item in workPost" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-button type="primary" size="small" @click="changeEmployee">确定</el-button>
      <el-button size="small" @click="closeShow">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getPeopleAPI } from '@/api/setting'
import { getUserInfoApi, changeEmployeeAPI } from '@/api/employees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      checkList: [],
      page: 1,
      pagesize: 999,
      workPost: [],
      loading: false
    }
  },
  created() {
  },
  methods: {
    // 修改员工的角色
    async changeEmployee() {
      await changeEmployeeAPI({ id: this.userId, roleIds: this.checkList })
      this.$message.success('修改成功')
      this.closeShow()
    },
    openDialog() {
      this.loading = true
      Promise.all([this.getPeople(), this.getOne()]).then(() => {
        this.loading = false
      })
    },
    async getPeople() {
      const { data: { rows }} = await getPeopleAPI({ page: this.page, pagesize: this.pagesize })
      this.workPost = rows
      // console.log(rows)
    },
    async getOne() {
      const { data } = await getUserInfoApi(this.userId)
      // console.log(data.roleIds)
      this.checkList = data.roleIds || []
    },
    closeShow() {
      this.$emit('update:showRoleDialog', false)
    }
  }
}
</script>
